// index.js
const app=getApp();
Page({
    data: {
      info:'',
      notices:[{
        id:1,
        title:'通知',
        content:'部分功能尚未完善，如出现功能异常，请联系告知管理员，谢谢！',
        adminName:'',
        date:'1921年9月10日 20时'
      },{
        id:2,
        title:'欢迎',
        content:'欢迎使用云小驿社区只能管理系统',
        adminName:'',
        date:'1921年9月10日 20时'
      }],
      noticeBG:'',// 背景图片
      communityPhone:'13133334468',
      functionList:[
          {
            icon:'icon-yao',
            title:'访客邀请',
            to:'/pages/yao/yao',
            like:'访客邀请邀请访客'
          },
          {
            icon:'icon-weixiuguanli',
            title:'故障报修',
            to:'/pages/repair/repair',
            like:'故障报修故障维修物业报修故障保修'
          },
          {
            icon:'icon-cheweigongxiang',
            title:'车位共享',
            to:'/pages/driveway/driveway',
            like:'车位共享'
          },
          {
            icon:'icon-lajifenlei',
            title:'垃圾分类',
            to:'/pages/garbageSorting/garbageSorting',
            like:'垃圾分类'
          },
          {
            icon:'icon-shenghuo-copy',
            title:'生活服务',
            to:'/pages/lifeService/lifeService',
            like:'生活服务日常服务'
          },
          // {
          //   icon:'icon-kuaidiyuan-xianxing',
          //   title:'快递上门订单',
          //   to:'/pages/expressOrder/expressOrder',
          //   like:'快递上门订单快递订单'
          // },
          // {
          //   icon:'icon-add',
          //   title:'发布邻里互助',
          //   to:'/pages/publishHelp/publishHelp',
          //   like:'邻居互助邻里互助发布'
          // },
          // {
          //   icon:'icon-yaoqingliebiao',
          //   title:'邀请卡列表',
          //   to:'/pages/yaoList/yaoList',
          //   like:'邀请卡列表邀请列表'
          // },

      ]
    },
    onLoad() {
        const that=this;
        // 请求公告背景图片
        wx.request({
          url:'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN',
          success: function(res){
              that.setData({
                noticeBG:'https://cn.bing.com'+res.data.images[0].url
              })
          },
          fail: function(res) {
              console.log(res);
          }
        })
        this.init();
    },
    onShow(){
        // 设置当前选中的tabbar
        if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
            this.getTabBar().setData({
              selected: 0
            })
        }
    },
    /** 更新当前页面内容 */
    init(){
      wx.request({
        url: app.globalData.host+'client/notice/topList',
        method:'GET',
        timeout:10000,
        success:res=>{
        
          res.data.data.topList.forEach(item=>{
            item.date=this.formatTime(item.date);
          })
          this.setData({
            notices:res.data.data.topList
          })
        },
        fail:()=>{
          wx.showToast({
            title: '服务器繁忙',
            icon:'error'
          })
        }
      })
    },
    formatTime(e){
      var time =  new Date(parseInt(e));
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      let t= y+'年'+m+'月'+d+'日';
      return t;
    },
    getUserProfile(e) {
      // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
      wx.getUserProfile({
        desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log(res)
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    },
    // 呼叫社区
    tellCommunity(){
      if(wx.getStorageSync('userId')==null || wx.getStorageSync('userId')==undefined || wx.getStorageSync('userId')==''){
        wx.showToast({
          title: '未登录',
          icon:'error'
        })
        return;
      }
      wx.makePhoneCall({
        phoneNumber: this.data.communityPhone,
      })
    },
    // 一键报警
    alarm(){
      if(wx.getStorageSync('userId')==null || wx.getStorageSync('userId')==undefined || wx.getStorageSync('userId')==''){
        wx.showToast({
          title: '未登录',
          icon:'error'
        })
        return;
      }
      wx.showToast({
        title: '报警信息已发送',
        mask:true,
        image:'/icon/alarm.png',
        icon:'error',
        duration:3000
      })
      wx.request({
        url: app.globalData.host+'/client/user/alarm?userId='+wx.getStorageSync('userId')+'&communityId='+wx.getStorageSync('userInfo').housingId
      })
    }
})